﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Engine
{
    public class Engine
    {
        /**this property represent lattice behind that*/
        private Lattice lattice;

        /**we have two modes of work. The problem of bonds and the problem of points */
        private int mode;

        public int Mode
        {
            get
            {
                return mode;
            }
            set 
            {
                mode = value;
            }
        }

        /**
         * this class responsible for all calculations in this programm 
         * its test our lattice for Xc - conductivity  treashold.
         * */
        public Engine(Lattices type)
        {
            switch((int)type)
            {
                case (int)Lattices.Cubic:
                    lattice = new Cubic();
                break;
                case (int)Lattices.Square:
                    lattice = new Square();
                break;
                case (int)Lattices.Triangular:
                    lattice = new Triangular();
                break;
            }
        }

        /** initializator for 2 dimensional lattice. */
        public Engine(Lattices type,int x,int y) : this(type)
        {
            lattice.X = x;
            lattice.Y = y;
        }

        /** initializator for 3 dimensional lattice. */
        public Engine(Lattices type,int x,int y,int z) : this(type,x,y)
        {
            lattice.Z = z;
        }

        public int X 
        {
            get { return lattice.X; }
            set { lattice.X = value; }        
        }

        public int Y 
        {
            get { return lattice.Y; }
            set { lattice.Y = value; }
        }

        public int Z 
        {
            get { return lattice.Z; }
            set { lattice.Z = value; }
        }

        public IList<double> repeatFindConductivityThreshold(int numberOfRepeat) 
        {
            IList<double> results = new List<double>();
            for (int i = 0; i < numberOfRepeat; i++ )
            {
                results.Add(findConductivityThreshold());
            }
            return results;
        }

        /**<summary>Find percolation threashold for this lattice.</summary>*/
        public double findConductivityThreshold()
        {
            //prepare lattice for next use
            lattice.Reset();
            bool isCoduct = true;
            while (isCoduct)
            {
                if((int)Modes.Point == mode)
                {
                    lattice.CutPoint();
                }
                else if((int)Modes.Bond == mode)
                {
                    lattice.CutBond();
                }
                else
                {
                    throw new ArgumentException("invalid mode of engine class");
                }
                isCoduct = lattice.IsConduct();
            }
            if((int)Modes.Point == mode)
            {
                return ((double)(lattice.WorkingPoints)) / (lattice.CuttedPoints + lattice.WorkingPoints);
            }
            else if((int)Modes.Bond == mode)
            {
                return ((double)(lattice.WorkingBonds)) / (lattice.CuttedBonds + lattice.WorkingBonds);
            }
            throw new ArgumentException("invalid mode of engine class");
         }

    }
}
